package com.config;

import com.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Servlet配置
 * @date: 2018/11/23 18:08
 */
@Configuration
public class GlobalServletConfig implements WebMvcConfigurer
{

    private static final String FAVICON_URL = "/favicon.ico";

    private static final String SWAGGER_URL = "/swagger-ui.html";

    private static final String SWAGGER_RESOURCES_URL = "/swagger-resources/**";

    private static final String KNIFE4J_URL = "/doc.html/**";

    private static final String KNIFE4J_CSS_RESOURCES_URL = "/webjars/css/**";

    private static final String KNIFE4J_JS_RESOURCES_URL = "/webjars/js/**";

    private static final String SWAGGER_API_DOC_URL = "/v2/api-docs";

    private static final String WEBJARS_URL = "/webjars/springfox-swagger-ui/**";

    private static final String ERROR_URL = "/error";

    private static final String FILES_URL = "/files/**";

    private static final List<String> EXCLUDE_PATH_LIST = new ArrayList<>();

    static
    {
        EXCLUDE_PATH_LIST.add(ERROR_URL);
        EXCLUDE_PATH_LIST.add(FAVICON_URL);
        EXCLUDE_PATH_LIST.add(SWAGGER_URL);
        EXCLUDE_PATH_LIST.add(SWAGGER_RESOURCES_URL);
        EXCLUDE_PATH_LIST.add(SWAGGER_API_DOC_URL);
        EXCLUDE_PATH_LIST.add(WEBJARS_URL);
        EXCLUDE_PATH_LIST.add(FILES_URL);
        EXCLUDE_PATH_LIST.add(KNIFE4J_URL);
        EXCLUDE_PATH_LIST.add(KNIFE4J_CSS_RESOURCES_URL);
        EXCLUDE_PATH_LIST.add(KNIFE4J_JS_RESOURCES_URL);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**").excludePathPatterns(EXCLUDE_PATH_LIST);
    }

    @Bean
    public AuthInterceptor authInterceptor()
    {
        return new AuthInterceptor();
    }

}
